﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Drawing;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZJ_BCKJBControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                BindData();
                QSDataDLL();
            }
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                CustomGridView1.Columns[13].Visible = false;
            }
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            Button bt_query = (Button)FindControl("bt_query");
            Button btnAdd = (Button)FindControl("btnAdd");
            Button bt_huazhuan = (Button)FindControl("bt_huazhuan");
            Button bt_daozhang = (Button)FindControl("bt_daozhang");
            Button btn_yinhang = (Button)FindControl("btn_yinhang");
            bt_query.Click += new EventHandler(btnSeach_Click);
            btnAdd.Click += new EventHandler(btnAdd_Click);
            bt_huazhuan.Click += new EventHandler(bt_huazhuan_Click);
            bt_daozhang.Click+=new EventHandler(bt_daozhang_Click);
            btn_yinhang.Click+=new EventHandler(btn_yinhang_Click);
            foreach (GridViewRow gvr in CustomGridView1.Rows) {
                Button btn_tj = (Button)gvr.FindControl("btn_tj");
                Button btn_tj2 = (Button)gvr.FindControl("btn_tj2");
                btn_tj.Click += new EventHandler(ChangeStatus);
                btn_tj2.Click += new EventHandler(ChangeStatus2);
            }
		}
		#region 获取补偿款资金信息数据
		public DataTable GetBCZJ(string strwhere1, string strwhere2)
        {
            string sql = string.Format(@"select p2.立项号,p2.项目名称,p2.pnid,
p.开户银行,  
p.报结情况, 
replace(p.到账金额, '\', '') 到账金额,  
replace(p.划转金额, '\', '') 划转金额,  
replace(p.余额, '\', '') 余额,  
replace(p.概算总金额, '\', '') 概算总金额,  
p.ID 编号, 
p.开户审核状态, 
p.ID  
  from (select 立项号, 项目名称, id pnid,评估机构,征收实施单位,区属 from zs_project p2 where 提醒开户状态=1 {0}) p2
  left join (select p.开户银行,
                    p.报结情况,
                    (select sum(to_number(nvl(replace(到账金额, '\', ''), 0)))
                       from zs_new_money
                      where bid = p.id) 到账金额,
                    (select sum(to_number(nvl(replace(划转金额, '\', ''), 0)))
                       from zs_change_money
                      where bid = p.id) 划转金额,
                    (select (to_number(nvl((select sum(to_number(nvl(replace(到账金额, '\', ''),
                                                                    0)))
                                             from zs_new_money
                                            where bid = p.id),
                                           0)) -
                            to_number(nvl((select sum(to_number(nvl(replace(划转金额, '\', ''),
                                                                    0)))
                                             from zs_change_money
                                            where bid = p.id),
                                           0)))
                       from dual) 余额,
                    to_number(nvl((select sum(replace(概算补偿总金额, '\', ''))
                                    from ZS_PGGS
                                   where pid = p.pnid),
                                  0)) 概算总金额,
                    p.id 编号,
                    p.id,
                    p.pnid,p.银行账号,p.时间,p.开户审核状态
               from ZS_BCBASICINFO p where 1=1 {1}) p on p.pnid = p2.pnid ", strwhere1, strwhere2);
            if (!string.IsNullOrEmpty(strwhere2))
            {
                sql = string.Format(@"select p2.立项号,
       p2.项目名称,
       p2.id pnid,
       p.开户银行,
       p.报结情况,
	   p.开户审核状态,
       (select sum(to_number(nvl(replace(到账金额, '\', ''), 0)))
          from zs_new_money
         where bid = p.id) 到账金额,
       (select sum(to_number(nvl(replace(划转金额, '\', ''), 0)))
          from zs_change_money
         where bid = p.id) 划转金额,
       (select (to_number(nvl((select sum(to_number(nvl(replace(到账金额, '\', ''), 0)))
                                from zs_new_money
                               where bid = p.id),
                              0)) -
               to_number(nvl((select sum(to_number(nvl(replace(划转金额, '\', ''), 0)))
                                from zs_change_money
                               where bid = p.id),
                              0)))
          from dual) 余额,
       to_number(nvl((select sum(replace(概算补偿总金额, '\', ''))
                       from ZS_PGGS
                      where pid = p.pnid),
                     0)) 概算总金额,
       p.ID 编号,
	   p.开户审核状态,
       p.ID
  from zs_project p2, ZS_BCBASICINFO p
 where p.pnid = p2.id {0} {1}", strwhere1, strwhere2);
            }
            DataTable dt = null;
			dt = _EluZSDataProvider.GetTable(sql);
            return dt;
        }
		#endregion

		public void BindData()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_OnRowCreated);
            CustomGridView1.RowDataBound += new GridViewRowEventHandler(CustomGridView1_RowDataBound);
            Label lbXmNum = (Label)FindControl("lbXmNum");
            Label lbdzje = (Label)FindControl("lbdzje");
            Label lbhzje = (Label)FindControl("lbhzje");
            Label lbye = (Label)FindControl("lbye");

            DataTable dt = GetBCZJ(getWhere1(), getWhere2());
            string xmNum = _EluZSDataProvider.GetCompensateBasicNum(getWhere1(), getWhere2());
            if (dt.Rows.Count > 0)
            {
                List<string> lis = new List<string>();
                lis.Add("到账金额");
                lis.Add("划转金额");
                lis.Add("余额");
                _EluZSDataProvider.AddCollectRow(dt, "编号", lis);
            }
            if (dt.Rows.Count > 0)
            {
                lbXmNum.Text = "项目总数为：" + xmNum + "个 |";
                lbdzje.Text = "合计到账金额：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["到账金额"]) + "元  ";
                lbhzje.Text = "合计划转金额：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["划转金额"]) + "元  | ";
                lbye.Text = "合计余额：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["余额"]) + "元";
                dt.Rows.RemoveAt(dt.Rows.Count - 1);
            }
            else
            {
                lbXmNum.Text = "";
                lbdzje.Text = "";
                lbhzje.Text = "";
                lbye.Text = "";
            }
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.DataKeyNames = new string[] { "ID", "报结情况" };
            CustomGridView1.DataBind();
        }
        protected void CustomGridView1_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            foreach (GridViewRow gvr in CustomGridView1.Rows)
            {
                if (!string.IsNullOrEmpty(CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString()))
                {
                    if (CustomGridView1.DataKeys[gvr.RowIndex].Values["报结情况"].ToString() == "0")
                    {
                        Button btn = (Button)gvr.FindControl("btn_tj");
                        btn.Enabled = true;
                    }
                    else
                    {
                        Button btn2 = (Button)gvr.FindControl("btn_tj2");
                        btn2.Enabled = true;
                    }
                }
            }
        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                //e.Row.Cells[12].Text = "开户";
                string strID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                string PNID = ((HiddenField)e.Row.FindControl("HiddenField2")).Value;
                //e.Row.Cells[12].Attributes.Add("style", "cursor:pointer;color:blue;");
                //e.Row.Cells[12].Attributes.Add("onclick", "javascript:window.location.href=('CompensationBasicInIframe.aspx?ID=" + strID + "&PID=" + PNID + "');");
                e.Row.Cells[13].Text = "查看";				
                if (string.IsNullOrEmpty(rowview["ID"].ToString()))
                {
                    e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:gray;");
                    e.Row.Cells[13].Attributes.Add("onclick", "");

                }
                else
                {
                    e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:blue;");
                    e.Row.Cells[13].Attributes.Add("onclick", "javascript:window.location.href=('ZJ_BCKJBInfo.aspx?ID=" + strID + "&PID=" + PNID + "');");
                }
                EluZSProvider.YJCS yjcs=_EluZSDataProvider.GetYJCS();

                string strYEJJZ = yjcs.余额警戒值;
                if (string.IsNullOrEmpty(strYEJJZ) || strYEJJZ == "&nbsp;")
                {
                    strYEJJZ = "0";
                }
                double yjc = Convert.ToDouble(strYEJJZ) / 100;
                string getxybck = _EluZSDataProvider.getXYBCK(PNID);
                if (string.IsNullOrEmpty(getxybck)) { getxybck = "0"; }
                double getye = Convert.ToDouble(getxybck);
                string strYED = yjcs.余额已低于警戒值;
                string strYEL = yjcs.账户余额为零;
				string khshzt = rowview["开户审核状态"].ToString();
                if (!string.IsNullOrEmpty(strID))
                {
					if (!string.IsNullOrEmpty(khshzt))
					{
						if (khshzt == "1")
						{
							for (int i = 0; i < e.Row.Cells.Count; i++)
							{
								e.Row.Cells[i].BackColor = Color.FromName("#BBE1F8");
							}
						}
						else {
							string shuoming = _EluZSDataProvider.GetSomeStr("ZS_BCBASICINFO", "说明", "id", strID);
							if (strYED == "1")
							{
								string temp = e.Row.Cells[11].Text.Trim();
								if (string.IsNullOrEmpty(temp) || temp == "&nbsp;") { temp = "0"; }
								double tt = Convert.ToDouble(temp);
								double mttt = getye * yjc;
								double tt1 = tt - mttt;//余额
								if (tt1 < 0)
								{
									if (string.IsNullOrEmpty(shuoming) || shuoming == "&nbsp;")
									{
										for (int i = 0; i < e.Row.Cells.Count; i++)
										{
											e.Row.Cells[i].BackColor = Color.FromName("#FE6501");
										}
									}
									else
									{
										//☆
										e.Row.Cells[4].Text = "<font color='#FE6501'>★</font>" + e.Row.Cells[4].Text;
									}
								}
							}
							if (strYEL == "1")
							{
								string temp = e.Row.Cells[11].Text.Trim();
								if (string.IsNullOrEmpty(temp) || temp == "&nbsp;") { temp = "0"; }
								double tt = Convert.ToDouble(temp);
								if (tt <= 0.0)
								{
									if (string.IsNullOrEmpty(shuoming.Trim()))
									{
										for (int i = 0; i < e.Row.Cells.Count; i++)
										{
											e.Row.Cells[i].BackColor = Color.FromName("Red");
										}
									}
									else
									{
										//◇
										e.Row.Cells[4].Text = "<font color='red'>◆</font>" + e.Row.Cells[4].Text;
									}
								}
							}
						}
					}
                    
                }
                else
                {
                    string tt1 = _EluZSDataProvider.GetSomeStr("zs_project", "提醒开户状态", "id", PNID);
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].BackColor = Color.FromName("Yellow");
                    }
                }
				
            }
        }
        private string getWhere1()
        {
            TextBox CQ_ITEM_ID = (TextBox)FindControl("CQ_ITEM_ID");
            TextBox xkzh = (TextBox)FindControl("xkzh");
            TextBox PGJG = (TextBox)FindControl("PGJG");
            TextBox ZSSSDW = (TextBox)FindControl("ZSSSDW");
            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            
            StringBuilder sbWhere = new StringBuilder();
            string strLXH = CQ_ITEM_ID.Text.Trim();
            string strXKZH = xkzh.Text.Trim();
            string strPGJG = PGJG.Text.Trim();
            string strZSDW = ZSSSDW.Text.Trim();
            string strQS = DropDownList1.SelectedValue;
            string resQS = HttpContext.Current.Request["qs"];
            if (!string.IsNullOrEmpty(strLXH)) { sbWhere.AppendFormat(" and p2.立项号 LIKE '%{0}%' ", strLXH); }
            if (!string.IsNullOrEmpty(strXKZH)) { sbWhere.AppendFormat(" and p2.id in (select pid from zs_prono where 征收许可证号 like '%{0}%') ", strXKZH); }
            if (!string.IsNullOrEmpty(strPGJG)) { sbWhere.AppendFormat(" and p2.评估机构 LIKE '%{0}%' ", strPGJG); }
            if (!string.IsNullOrEmpty(strZSDW)) { sbWhere.AppendFormat(" and p2.征收实施单位 LIKE '%{0}%' ", strZSDW); }
            if (!string.IsNullOrEmpty(strQS)) { sbWhere.AppendFormat(" and p2.区属 = '{0}'", strQS); }
            if (!string.IsNullOrEmpty(resQS)) { sbWhere.AppendFormat(" and p2.区属 like '%{0}%'", resQS); }
            return sbWhere.ToString();
        }
        private string getWhere2()
        {
            TextBox KHYH = (TextBox)FindControl("KHYH");
            TextBox YHZH = (TextBox)FindControl("YHZH");
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            DropDownList DropDownList2 = (DropDownList)FindControl("DropDownList2");

            StringBuilder sbWhere = new StringBuilder();
            string strKHYH = KHYH.Text.Trim();
            string strYHZH = YHZH.Text.Trim();
            string strBJ = DropDownList2.SelectedValue;
            string strYear = TextBox1.Text;
            if (!string.IsNullOrEmpty(strKHYH)) { sbWhere.AppendFormat(" and p.开户银行 LIKE '%{0}%' ", strKHYH); }
            if (!string.IsNullOrEmpty(strYHZH)) { sbWhere.AppendFormat(" and p.银行账号 LIKE '%{0}%' ", strYHZH); }
            if (!string.IsNullOrEmpty(strBJ)) { sbWhere.AppendFormat(" and p.报结情况 = '{0}'", strBJ); }
            if (!string.IsNullOrEmpty(strYear)) { sbWhere.AppendFormat(" and to_char(p.时间,'yyyy') like '%{0}%'", strYear); }
            return sbWhere.ToString();
        }
        protected void btnSeach_Click(object sender, EventArgs e)
        {
            BindData();
        }
        //新增
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("CompensationBasicInIframe.aspx");
        }
        //划转统计
        protected void bt_huazhuan_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZJ_HZTJ.aspx");
        }
        //到帐统计
        protected void bt_daozhang_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZJ_DZTJ.aspx");
        }
        //银行汇总
        protected void btn_yinhang_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZJ_YHHZ.aspx");
        }
        /// <summary>
        /// 绑定下拉框
        /// </summary>
        protected void QSDataDLL()
        {
            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            DropDownList DropDownList2 = (DropDownList)FindControl("DropDownList2");
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("区属");
            EluZSProvider.Params item = new Params();
            _params.Insert(0, item);
            DropDownList1.DataSource = _params;
            DropDownList1.DataValueField = "keyname";
            DropDownList1.DataTextField = "keyname";
            DropDownList1.DataBind();
            List<EluZSProvider.Params> _params1 = _EluZSDataProvider.GetParams("报结情况");
            EluZSProvider.Params item1 = new Params();
            _params1.Insert(0, item1);
            DropDownList2.DataSource = _params1;
            DropDownList2.DataValueField = "keyvalue";
            DropDownList2.DataTextField = "keyname";
            DropDownList2.DataBind();
        }
        /// <summary>
        /// 报结情况更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus(object sender, EventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string BID = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.ChangeZT("ZS_BCBASICINFO", "报结情况", BID, "1");
            BindData();
        }
        /// <summary>
        /// 撤消报结情况更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus2(object sender, EventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string BID = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.ChangeZT("ZS_BCBASICINFO", "报结情况", BID, "0");
            if (change) { BindData(); }
        }
    }
}


